package com.keldian.mobilesafe.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

/**
 * 常用号码查询数据库封装
 *
 * @author Kevin
 */
public class CommonNumberDao {

    private static final String PATH = "/data/data/com.keldian.mobilesafe/files/commonnum.db";

    /**
     * 获取所有组和孩子的信息
     *
     * @return
     */
    public static ArrayList<GroupInfo> getCommonNumberGroups() {
        SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH, null,
                SQLiteDatabase.OPEN_READONLY);// 只接收data/data路径下的数据库文件

        Cursor cursor = database.rawQuery("select name,idx from classlist",
                null);

        ArrayList<GroupInfo> list = new ArrayList<GroupInfo>();
        while (cursor.moveToNext()) {
            GroupInfo info = new GroupInfo();
            String name = cursor.getString(0);
            String idx = cursor.getString(1);
            info.name = name;
            info.idx = idx;
            info.children = getCommonNumberChildren(idx, database);
            list.add(info);
        }

        cursor.close();
        database.close();// 关闭数据库

        return list;
    }

    /**
     * 获取某个组的孩子列表
     */
    public static ArrayList<ChildInfo> getCommonNumberChildren(String idx,
                                                               SQLiteDatabase database) {
        Cursor cursor = database.rawQuery(
                "select number,name from table" + idx, null);

        ArrayList<ChildInfo> list = new ArrayList<ChildInfo>();
        while (cursor.moveToNext()) {
            ChildInfo info = new ChildInfo();
            String name = cursor.getString(1);
            String number = cursor.getString(0);
            info.name = name;
            info.number = number;

            list.add(info);
        }

        return list;
    }

    public static class GroupInfo {
        public String name;
        public String idx;
        public ArrayList<ChildInfo> children;
    }

    public static class ChildInfo {
        public String name;
        public String number;
    }
}
